package tp.pr5.instructions;

import tp.pr5.Constants;
import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.Item;
import tp.pr5.items.ItemContainer;

/**
 * This instruction drops an Item from the current place and puts it the robot inventory.
 * This instruction works if the user writes DROP or SOLTAR
 *
 */
public class DropInstruction implements Instruction
{

	private NavigationModule navigation;
	private ItemContainer robotContainer;
	private String id;
	private RobotEngine engine;

	/**
	 * Default constructor
	 * 
	 */
	
	public DropInstruction()
	{
		this.navigation = null;
		this.robotContainer = null;
	}
	
	/**
	 * Constructor with an id
	 * 
	 * @param id
	 */
	
	public DropInstruction(String id)
	{
		this();
		this.id = id;
	}
	
	/**
	 * Parses the String returning an instance of DropInstruction or throwing a WrongInstructionFormatException()
	 * 
	 * @param cad string given
	 */
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException 
	{
		String[] entry = cad.trim().split(" +");
		if((entry[0].equalsIgnoreCase("DROP") || entry[0].equalsIgnoreCase("SOLTAR")) && entry.length == 1)
		{
			throw new WrongInstructionFormatException(); 
		}
		if((entry[0].equalsIgnoreCase("DROP") || entry[0].equalsIgnoreCase("SOLTAR")) && entry.length == 2)
		{
			return new DropInstruction(entry[1]);
		}
		throw new WrongInstructionFormatException();
	}

	/**
	 * Fixes the current context, i.e., the robot engine and the navigation module
	 * 
	 * @param engine RobotEngine
	 * @param navigation NavigationModule
	 * @param robotContainer ItemContainer
	 * 
	 */
	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) 
	{
		this.navigation = navigation;
		this.robotContainer = robotContainer;
		this.engine = engine;
	}

	/**
	 * The robot drops an Item from its inventory in the current place, if the item exists
	 */
	@Override
	public void execute() throws InstructionExecutionException 
	{
		try
		{
			Item item = this.robotContainer.pickItem(this.id);
			
			//Checks if the item exists
			if(item != null)
			{
				//Checks if the item is in the place already
				if(navigation.findItemAtCurrentPlace(this.id))
				{
					throw new InstructionExecutionException(Constants.MESSAGE_DROP_ERROR2.replace("{ID}", this.id));
				}
				else 
				{
					this.navigation.dropItemAtCurrentPlace(item);
					this.engine.saySomething(Constants.MESSAGE_DROP_OK.replace("{ID}", this.id));
				}
			}
			else
			{
				throw new InstructionExecutionException(Constants.MESSAGE_DROP_ERROR.replace("{ID}", this.id));
			}
			
		} catch(Exception e){
			throw e;
		}
	}

	/**
	 * Instruction help
	 */
	@Override
	public String getHelp()
	{
		return "DROP|SOLTAR <id>";
	}
	
}
